/*
 * Copyright(C) 2010 the original author or authors.
 *
 * Change History Log
 * ----------------------------------------------------
 * Version: 1.0
 * Date: 2011-5-22
 * Author: wangzl
 * ----------------------------------------------------
 */
package com.seemsea.dao.impl.admin;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.seemsea.dao.AbstractGenericDAO;
import com.seemsea.dao.admin.CouponDao;
import com.seemsea.entity.admin.coupon.Coupon;
import com.seemsea.entity.admin.coupon.Product;

/**
 * 
 * 
 * @author wangzl
 * @since 2011-5-22
 */
@Repository
public class CouponDaoImpl extends AbstractGenericDAO<Coupon, String> implements CouponDao {
	@Override
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}
	
	@SuppressWarnings("unchecked")
	public List<Coupon> getCouponByProduct(Product product){
		StringBuffer paramsBuffer = new StringBuffer();
		if(product != null){
			if(product.getCode() != null && !product.getCode().equals("")){
				paramsBuffer.append(" and c.product_id = '"+product.getCode()+"' ");
			}
		}
		StringBuffer sqlBuffer = new StringBuffer("select distinct c.* from coupon_coupon c " +
				 " where now() between startdate and enddate ");
		sqlBuffer.append(paramsBuffer);
		return entityManager.createNativeQuery(sqlBuffer.toString(), Coupon.class).getResultList();
	}
}
